package com.tszs.Controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.tszs.biz.CommodityListBiz;
import com.tszs.biz.DistributionAllocationBiz;
import com.tszs.entity.CommodityList;
import com.tszs.entity.DistributionAllocation;
import com.tszs.util.CallBack;
import com.tszs.util.Uuid;

import net.sf.json.JSONObject;

/**
 * @类名:CommodityListCpontroller
 * @类描述：商品管理 @创建时间：2019-03-22 13:07:07
 * @创建人：王俊
 * @author Administrator
 *
 */
@Controller
public class DistributionAllocationController {
	/**
	 * @DistributionAllocationBiz
	 * @描述:分销配置
	 * @创建时间:201903-21 13:14:14
	 */
	@Autowired
	private DistributionAllocationBiz distributionAllocationBiz;
	/**
	 * @CommodityListBiz
	 * @描述:商品查询
	 * @创建时间:201903-21 13:14:14
	 */
	@Autowired
	private CommodityListBiz commodityListBiz;

	/**
	 * @方法名:DistributionConfigurationAddition
	 * @方法描述：分销配置添加 @创建时间：2019-03-22 13:07:07
	 * @创建人：王俊
	 * @author Administrator
	 *
	 */
	@RequestMapping("pc/admin/DistributionConfigurationAddition")
	public void DistributionConfigurationAddition(HttpServletRequest request, HttpServletResponse response) {
		DistributionAllocation record = new DistributionAllocation();
		record.setdId(Uuid.getUUID());
		record.setdCreateTime(Uuid.getTime());
		record.setcId(request.getParameter("cId"));
		record.setFirstClassCommission(request.getParameter("FirstClassCommission"));
		record.setTwoStageReturnCommission(request.getParameter("TwoStageReturnCommission"));
		record.setThreeStageReturnCommission(request.getParameter("ThreeStageReturnCommission"));
		record.setdStatic("0");
		record.setUserId(request.getSession().getAttribute("aId") + "");
		record.setUserName(request.getSession().getAttribute("aUsername") + "");
		int c = distributionAllocationBiz.insertSelective(record);
		if (c > 0) {
			new CallBack().yes(response, "", "");
		} else {
			new CallBack().no(response, "", "");
		}
	}

	/**
	 * @方法名:DistributionConfigurationAddition
	 * @方法描述：分销配置添加 @创建时间：2019-03-22 13:07:07
	 * @创建人：王俊
	 * @author Administrator
	 *
	 */
	@RequestMapping("pc/admin/distributionGoodsInquiry")
	public void distributionGoodsInquiry(HttpServletRequest request, HttpServletResponse response) {
		CommodityList example = new CommodityList();
		List<CommodityList> selectByIdName = commodityListBiz.selectByIdName(example);
		if (selectByIdName.size() > 0) {
			new CallBack().yes(response, selectByIdName, "");
		} else {
			new CallBack().no(response, selectByIdName, "");
		}
	}

	/**
	 * @方法名:distributionPaging
	 * @方法描述：商家账号明细 @创建时间：2019-03-22 13:07:07
	 * @创建人：王俊
	 * @author Administrator
	 *
	 */
	@RequestMapping("pc/admin/distributionPaging")
	public void findCommodityListPage(HttpServletRequest request, HttpServletResponse response) {
		response.setContentType("text/xml; charset=UTF-8");
		response.setContentType("text/html;charset=utf-8");
		int page = Integer.parseInt(request.getParameter("page"));
		int limit = Integer.parseInt(request.getParameter("limit"));
		String begin = request.getParameter("type3");
		String end = request.getParameter("type4");
		DistributionAllocation example = new DistributionAllocation();
		example.setPage((page - 1) * limit);
		example.setLimit(limit);
		if (!(null == begin) && !(null == end)) {
			if (!"".equals(begin) && !"".equals(end)) {
				begin = begin + " 00:00:01";
				end = end + " 23:59:59";
				example.setBegin(begin);
				example.setEnd(end);
			}
		}
		List<DistributionAllocation> selectByExample = distributionAllocationBiz.selectByExamplePage(example);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("code", 0);
		map.put("msg", "");
		map.put("count", selectByExample.size());
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		for (int i = 0; i < selectByExample.size(); i++) {
			Map<String, Object> mapData = new HashMap<String, Object>();
			mapData.put("id", i+1);
			mapData.put("cId", selectByExample.get(i).getcId());
			mapData.put("FirstClassCommission", selectByExample.get(i).getFirstClassCommission());
			mapData.put("TwoStageReturnCommission", selectByExample.get(i).getTwoStageReturnCommission());
			mapData.put("ThreeStageReturnCommission", selectByExample.get(i).getThreeStageReturnCommission());
			mapData.put("CreateTime", selectByExample.get(i).getdCreateTime());
			mapData.put("UserId", selectByExample.get(i).getUserId());
			mapData.put("UserName", selectByExample.get(i).getUserName());
			mapData.put("button","<a href=\"javascript:table('1','"
					+ selectByExample.get(i).getdId() + "')\" class=\"layui-btn layui-btn-xs\">删除</a><a href=\"javascript:table('0','"
							+ selectByExample.get(i).getdId() + "')\"  class=\"layui-btn layui-btn-danger layui-btn-xs\">修改</a>"
					);
			list.add(mapData);
		}
		map.put("data", list);
		JSONObject json = JSONObject.fromObject(map);
		new CallBack().textObject(json, response);
	}
}
